//
//  GameCenterViewController.m
//  酷狗游戏中心
//
//  Created by 卢泽添 on 15/7/15.
//  Copyright (c) 2015年 卢泽添. All rights reserved.
//

#import "GameCenterViewController.h"
#import "NewsTopTableViewCell.h"
#import "GameListTableViewCell.h"
#import "GameDetailViewController.h"
@interface GameCenterViewController ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *myPicScrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *myPageControl;
@property (strong,nonatomic) NSTimer *timer;
@property (strong,nonatomic)NSArray *adImageArray;
@property (weak, nonatomic) IBOutlet UITableView *gameList_Tableview;

@end

@implementation GameCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _adImageArray = @[@"u0.jpg",@"u1.jpg",@"u2.jpg",@"u3.jpg",@"u4.jpg"];
    _timer = [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    [self setupScrollViewAndmyPageControl];
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark --all logic code

- (void)setupScrollViewAndmyPageControl
{
    _myPageControl.numberOfPages = 5;
    //NSLog(@"[_adImageArray count]:%d",[_adImageArray count]);
    [self setupScrollView];
}

- (void)setupScrollView
{
    NSUInteger count = 5;
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = _myPicScrollView.frame.size.height;
    
    
    for (NSUInteger i = 0; i < count; i++) {
        CGRect frame = CGRectMake(i * width, 0, width, height);
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:frame];
        //imageView.image = [_recommendActs objectAtIndex:i];
        
        NSString *imageUrlStr  = _adImageArray[i];
        // NSLog(@"%@",imageUrlStr);
        // [imageView sd_setImageWithURL:[NSURL URLWithString:imageUrlStr] placeholderImage:[UIImage imageNamed:@"placekitty"]];
        imageView.image = [UIImage imageNamed:imageUrlStr];
        [_myPicScrollView addSubview:imageView];
        
    }
    
    [_myPicScrollView setContentSize:CGSizeMake(width * count, height)];
    _myPicScrollView.pagingEnabled = YES;
    _myPicScrollView.delegate = self;
}

- (void)nextPage
{
    NSInteger currentPage = _myPageControl.currentPage;
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = _myPicScrollView.frame.size.height;
    
    if (currentPage == 5-1) {
        CGRect frame = CGRectMake(0, 0, width, height);
        [_myPicScrollView scrollRectToVisible:frame animated:YES];
        _myPageControl.currentPage=0;
    }
    else{
        CGRect frame = CGRectMake((currentPage+1)*width, 0, width, height);
        [_myPicScrollView scrollRectToVisible:frame animated:YES];
        _myPageControl.currentPage++;
    }
}

#pragma mark -- all delegate action

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //NSLog(@"scrollViewScroll");
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    _myPageControl.currentPage = _myPicScrollView.contentOffset.x/width;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 6;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)
    {
        static NSString *CellIdentifier =@"newsTop";
        NewsTopTableViewCell *cell = [self.gameList_Tableview dequeueReusableCellWithIdentifier:CellIdentifier];
        cell.imageView.image = [UIImage imageNamed:@"u0.jpg"];
        cell.textLabel.text  = @"熊大赛破啊又要来啦。";
        return cell;
    }else if(indexPath.section>1)
    {
        static NSString *CellIdentifier =@"gameList";
        GameListTableViewCell *cell = [self.gameList_Tableview dequeueReusableCellWithIdentifier:CellIdentifier];
        return cell;
    }else
        return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 20;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)
    {
        return 30;
    }else
    {
        return 88;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GameDetailViewController *gameDetail = [self.storyboard instantiateViewControllerWithIdentifier:@"gameDetail"];
    [self.navigationController pushViewController:gameDetail animated:YES];
}
#pragma mark -- all IBAction


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
